//
//  WYAlertHelper.m
//  DeveDemo
//
//  Created by longtu on 15/5/14.
//  Copyright (c) 2015年 longtu. All rights reserved.
//

#import "WYAlertHelper.h"

@interface WYAlertHelper () <UIAlertViewDelegate, UIActionSheetDelegate>
/*  alertView
 *  注：不建议直接访问此属性
 */
@property (nonatomic,strong,readonly) UIAlertView *alertView;

/*  actionSheet
 *  注：不建议直接访问此属性
 */
@property (nonatomic,strong,readonly) UIActionSheet *actionSheet;

/* block回调 */
@property (nonatomic,copy) ClickedButtonBlock clickedButtonBlock;

@end

@implementation WYAlertHelper

+(void)showWithAleetViewType:(WYAleetViewType )aleetViewType inController:(UIViewController *)controller title:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles clickedButtonBlock:(ClickedButtonBlock)clickedButtonBlock{
    //创建对象
    WYAlertHelper *alertHelper = [[WYAlertHelper alloc] init];
    //加入子控制器，保住自己的命
    [controller addChildViewController:alertHelper];
    //记录type
    alertHelper.aleetViewType=aleetViewType;
    //根据不同的type创建视图
    [alertHelper madeViewWithAleetViewType:aleetViewType title:title message:message cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles];
    //记录block
    alertHelper.clickedButtonBlock=clickedButtonBlock;
}

#pragma mark - 根据不同的类型创建视图
-(void)madeViewWithAleetViewType:(WYAleetViewType )aleetViewType title:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles{
    if(WYAleetViewTypeUIAlertView == aleetViewType){
        // 创建alertView
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil];
        // 记录
        _alertView=alertView;
        // 添加按钮
        [_alertView addButtonWithTitle:destructiveButtonTitle];
        // 添加其他标题
        if(otherButtonTitles!=nil && otherButtonTitles.count!=0){
            for (NSString *otherButtonTitle in otherButtonTitles) {
                [_alertView addButtonWithTitle:otherButtonTitle];
            }
        }
        // 显示
        dispatch_async(dispatch_get_main_queue(), ^{
            [alertView show];
        });
    }else if(WYAleetViewTypeUIActionSheet == aleetViewType){
        // 创建actionSheet
        UIActionSheet *actionSheet=[[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:nil otherButtonTitles:nil, nil];
        // 记录
        _actionSheet=actionSheet;
        // 添加按钮
        [_actionSheet addButtonWithTitle:destructiveButtonTitle];
        // 添加其他标题
        if(otherButtonTitles!=nil && otherButtonTitles.count!=0){
            for (NSString *otherButtonTitle in otherButtonTitles) {
                [_actionSheet addButtonWithTitle:otherButtonTitle];
            }
        }
        // 显示
        dispatch_async(dispatch_get_main_queue(), ^{
            [actionSheet showInView:self.parentViewController.view];
        });
    }
}


#pragma mark - Delegate
/* UIAlertView */
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    dispatch_async(dispatch_get_main_queue(), ^{
        if(_clickedButtonBlock!=nil) _clickedButtonBlock(buttonIndex);
        //释放自己
        [self removeFromParentViewController];
    });
}

/* UIActionSheet */
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
/*
    //index修复
    //获取总共的index
    NSInteger count=actionSheet.numberOfButtons;
    if(buttonIndex!=0){
        if(buttonIndex>=count-1){
            buttonIndex=1;
        }else{
            buttonIndex=buttonIndex+1;
        }
    }
*/
    dispatch_async(dispatch_get_main_queue(), ^{
        if(_clickedButtonBlock!=nil) _clickedButtonBlock(buttonIndex);
        //释放自己
        [self removeFromParentViewController];
    });
}

@end
